package routers

import (
	"exporter/controller"
	"exporter/setting"
	"github.com/gin-gonic/gin"
	"io/ioutil"
	"net/http"
	//"github.com/prometheus/client_golang/prometheus/promhttp"
)

func SetupRouter() *gin.Engine {
	if setting.Conf.Release {
		gin.SetMode(gin.ReleaseMode)
	}
	r := gin.Default()
	// 告诉gin框架模板文件引用的静态文件去哪里找
	r.Static("/ui", "static/ui")

	// 告诉gin框架去哪里找模板文件
	r.LoadHTMLGlob("templates/*")
	//r.GET("/", controller.IndexHandler)
	r.GET("/metrics", controller.GetMetrics)

	// v1
	v1Group := r.Group("v1")
	{
		// 待办事项
		// 添加
		v1Group.POST("/prc", controller.CreateProcess)
		// 查看所有的待办事项
		v1Group.GET("/prc", controller.GetProcessList)
		v1Group.GET("/prc/:id", controller.GetAProcess)

		// 修改某一个待办事项
		v1Group.PUT("/prc/:id", controller.UpdateAProcess)
		// 删除某一个待办事项
		v1Group.DELETE("/prc/:id", controller.DeleteAProcess)
		v1Group.DELETE("/prc", controller.DeleteProcess)

		//	v1Group.DELETE("/prc/:id", controller.DeleteAProcess)

		v1Group.GET("/start/:id", controller.StartAProcess)
		v1Group.GET("/stop/:id", controller.StopAProcess)
		v1Group.GET("/exporter", controller.GetExporterInfo)

		v1Group.GET("/logs/:id", controller.GetProcessLog)

		v1Group.POST("/fileUpload", controller.UploadFile)
		v1Group.POST("/filesUpload", controller.UploadFiles)
	}

	// 关键点【解决页面刷新404的问题】
	r.NoRoute(func(c *gin.Context) {
		//设置响应状态
		c.Writer.WriteHeader(http.StatusOK)
		//载入首页
		indexHTML, _ := ioutil.ReadFile("static/ui/index.html") //vue.Asset(staticPath + "index.html")
		c.Writer.Write(indexHTML)
		//响应HTML类型
		c.Writer.Header().Add("Accept", "text/html")
		//显示刷新
		c.Writer.Flush()
	})

	return r
}
